package cn.stylefeng.guns.modular.school.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceInfo;
import cn.stylefeng.guns.modular.school.mapper.ParkingPlaceInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.ParkingPlaceInfoParam;
import cn.stylefeng.guns.modular.school.model.params.ParkingPlaceUseInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ParkingPlaceInfoResult;
import cn.stylefeng.guns.modular.school.model.result.ParkingPlaceUseInfoResult;
import  cn.stylefeng.guns.modular.school.service.ParkingPlaceInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceUseInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 停车位信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-15
 */
@Service
public class ParkingPlaceInfoServiceImpl extends ServiceImpl<ParkingPlaceInfoMapper, ParkingPlaceInfo> implements ParkingPlaceInfoService {

    @Autowired
    private ParkingPlaceUseInfoService parkingPlaceUseInfoService;

    @Override
    public void add(ParkingPlaceInfoParam param){
        ParkingPlaceInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ParkingPlaceInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ParkingPlaceInfoParam param){
        ParkingPlaceInfo oldEntity = getOldEntity(param);
        ParkingPlaceInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ParkingPlaceInfoResult findBySpec(ParkingPlaceInfoParam param){
        return null;
    }

    @Override
    public List<ParkingPlaceInfoResult> findListBySpec(ParkingPlaceInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ParkingPlaceInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<ParkingPlaceInfoResult> records = page.getRecords();
        records.forEach(parkingPlaceInfoResult -> {
            ParkingPlaceUseInfoParam parkingPlaceUseInfoParam = new ParkingPlaceUseInfoParam();
            parkingPlaceUseInfoParam.setParkingPlaceId(parkingPlaceInfoResult.getSeqId());
            ParkingPlaceUseInfoResult parkingPlaceUseInfoResult = parkingPlaceUseInfoService.sumByParkingPlaceId(parkingPlaceUseInfoParam);
            if(parkingPlaceUseInfoResult!=null) {
                parkingPlaceInfoResult.setSumMoney(parkingPlaceUseInfoResult.getMoney());
            }
            parkingPlaceInfoResult.setTimeStartStr(DateUtil.format(parkingPlaceInfoResult.getTimeStart(), "HH:mm"));
            parkingPlaceInfoResult.setTimeEndStr(DateUtil.format(parkingPlaceInfoResult.getTimeEnd(), "HH:mm"));
        });

        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ParkingPlaceInfoParam param){
        return param.getSeqId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ParkingPlaceInfo getOldEntity(ParkingPlaceInfoParam param) {
        return this.getById(getKey(param));
    }

    private ParkingPlaceInfo getEntity(ParkingPlaceInfoParam param) {
        ParkingPlaceInfo entity = new ParkingPlaceInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
